package com.snacks;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;

import com.snacks.data.DataFromHttpSever;
import com.snacks.data.URLconstan;
import com.snacks.utils.AccentRemover;

public class Search extends Activity implements OnClickListener {
	private Button hienthi;
	private Spinner sp_searchCity, sp_searchDistrict;
	private int posCity;
	private String strCity, strDistric;
	private ProgressDialog pDialog;
	private AutoCompleteTextView mSearch;
	
	private ArrayList<String> mData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search);
		
		mData = new ArrayList<String>();
		mSearch = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);

		// parse json data
		JSONArray jArray = DataFromHttpSever.getJSONfromURL(URLconstan
				.getTopItemService());

		try {			
			Log.i("log_tag", "Data Length: " + jArray.length());
			for (int i = 0; i < jArray.length(); i++) {				
				
				JSONObject e = jArray.getJSONObject(i);
				String name = AccentRemover.removeAccent(e.getString("name"));
				name = name.replace("-", " ");
				mData.add(name);	
				Log.i("log_tag","Name: " + name +", address: "+ e.getString("address"));
			}
		} catch (JSONException e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, mData);

		mSearch.setAdapter(adapter);
		

		mSearch.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Log.i("SELECTED TEXT WAS------->", mData.get(arg2));
                Intent myIntent = new Intent(Search.this, SearchDetail.class);
    			Bundle mBundle = new Bundle();

    			mBundle.putString("name", arg0.getItemAtPosition(arg2).toString());
    			mBundle.putString("district", strDistric);			
    			mBundle.putString("city", strCity);
    			myIntent.putExtras(mBundle);

    			startActivity(myIntent);
            }
        });

		hienthi = (Button) findViewById(R.id.btSearchView);
		hienthi.setOnClickListener(this);

		sp_searchCity = (Spinner) findViewById(R.id.spinnerCity);
		sp_searchDistrict = (Spinner) findViewById(R.id.spinnerDistrict);

		// xu li spinner chon Thanh pho
		ArrayAdapter<CharSequence> adapterCity = ArrayAdapter
				.createFromResource(this, R.array.danhsachthanhpho,
						android.R.layout.simple_spinner_item);
		adapterCity
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_searchCity.setAdapter(adapterCity);
		sp_searchCity.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				strCity = arg0.getItemAtPosition(arg2).toString();
				posCity = arg2;
				if (posCity >= 0)
					selectDistrict();
			}

			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

	}

	// xu li spinner chon quan phu thuoc vao spinner chon thanh pho
	public void selectDistrict() {
		if (posCity == 0) {
			ArrayAdapter<CharSequence> adapterQuan = ArrayAdapter
					.createFromResource(this, R.array.quan_hanoi,
							android.R.layout.simple_spinner_item);
			adapterQuan
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp_searchDistrict.setAdapter(adapterQuan);
		}
		if (posCity == 1) {
			ArrayAdapter<CharSequence> adapterQuan = ArrayAdapter
					.createFromResource(this, R.array.quan_hochiminh,
							android.R.layout.simple_spinner_item);
			adapterQuan
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp_searchDistrict.setAdapter(adapterQuan);
		}
		if (posCity == 2) {
			ArrayAdapter<CharSequence> adapterQuan = ArrayAdapter
					.createFromResource(this, R.array.quan_danang,
							android.R.layout.simple_spinner_item);
			adapterQuan
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp_searchDistrict.setAdapter(adapterQuan);
		}		
		if (posCity == 4) {
			ArrayAdapter<CharSequence> adapterQuan = ArrayAdapter
					.createFromResource(this, R.array.quan_haiphong,
							android.R.layout.simple_spinner_item);
			adapterQuan
					.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			sp_searchDistrict.setAdapter(adapterQuan);
		}		
		sp_searchDistrict
				.setOnItemSelectedListener(new OnItemSelectedListener() {

					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						strDistric = arg0.getItemAtPosition(arg2).toString();
					}

					public void onNothingSelected(AdapterView<?> arg0) {

					}
				});
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btSearchView:
			Intent myIntent = new Intent(Search.this, SearchDetail.class);
			Bundle mBundle = new Bundle();

			mBundle.putString("city", strCity);
			mBundle.putString("district", strDistric);			
			mBundle.putString("city", "");
			myIntent.putExtras(mBundle);

			startActivity(myIntent);

			break;

		default:
			break;
		}
	}

	// tao lop NewSearch
	class NewSearch extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(Search.this);
			pDialog.setMessage("Tìm kiếm...!");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		protected String doInBackground(String... arg0) {
			String tp = strCity;
			String q = strDistric;

			// post len API
			List<NameValuePair> listRoom = new ArrayList<NameValuePair>();
			listRoom.add(new BasicNameValuePair("city", tp));
			listRoom.add(new BasicNameValuePair("district", q));

			return null;

		}

	}
}
